<?php
// $Id: to_do_block.module,v 1.1.2.7 2010/08/04 09:17:36 alexiswilke Exp $

// this is the default, go to the block settings to change in UI
define('TO_DO_HELP_LINK', 'http://www.m2osw.com/doc_to_do_list_item');

// default number of items per page
define('TO_DO_BLOCK_DEFAULT_ITEMS_PER_PAGE', 5);

/**
 * Loads an array with the specified user preferences.
 */
function _to_do_user_preferences($uid) {
  $preferences = array();
  if ($uid) {
    $sql = "SELECT * FROM {to_do_block_user_preferences} WHERE uid = %d";
    $preferences = db_fetch_array(db_query($sql, $uid));
    if (!is_array($preferences)) {
      $preferences = array();
    }
  }
  $preferences += array(
    'sidebar_items' => TO_DO_BLOCK_DEFAULT_ITEMS_PER_PAGE,
    'low_priority_items_display' => 0,
  );
  if ($preferences['sidebar_items'] < 1) {
    $preferences['sidebar_items'] = TO_DO_BLOCK_DEFAULT_ITEMS_PER_PAGE;
  }
  return $preferences;
}

/**
 * Get the current selection.
 *
 * This function determines the current selection that will represent the
 * default selection.
 *
 * The block gives the user a way to choose between the current selection,
 * Urgent and All To do items. This function returns one of those values.
 *
 * \param[in] $selection Used as the new type if valid and the tab selection is not TO_DO_SELECTION_LAST_SELECTION.
 */
function to_do_last_selection($selection = NULL) {
  $system_selection = variable_get('to_do_block_list_selection', TO_DO_SELECTION_LAST_SELECTION);
  if ($system_selection != TO_DO_SELECTION_LAST_SELECTION) {
    $selection = $system_selection;
  }
  elseif (is_null($selection)) {
    if (isset($_SESSION['to_do_selection'])) {
      $selection = $_SESSION['to_do_selection'];
    }
    else {
      $selection = TO_DO_SELECTION_URGENT;
    }
  }

  // make sure that the value is an acceptable To do selection
  switch ((string)$selection) {
  case (string)TO_DO_SELECTION_URGENT:
  case (string)TO_DO_SELECTION_ALL:
    break;

  case 'all': // json sends 'all' instead of 1
    $selection = TO_DO_SELECTION_ALL;
    break;

  default: // json sends 'urgent' instead of 0
    $selection = TO_DO_SELECTION_URGENT;
    break;

  }

  // save in case the users goes to a new page and
  // the default selection is TO_DO_SELECTION_LAST_SELECTION
  $_SESSION['to_do_selection'] = $selection;

  return $selection;
}

/**
 * Implementation of hook_menu().
 *
 * Add the json menu entry so one can use AJAX calls from
 * the block.
 */
function to_do_block_menu() {
  $menu = array();

  // block
  $menu['to_do/json/%/%'] = array(
    'title' => 'to do list block callback',
    'description' => 'Provides a callback function for the ajax features of the to do list block',
    'page callback' => 'to_do_block_json',
    'page arguments' => array(2, 3),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
    'file' => 'to_do_block.pages.inc',
  );

  return $menu;
}

/**
 * Implementation of hook_user().
 */
function to_do_block_user($op, &$edit, &$account, $category = NULL) {
  module_load_include('users.inc', 'to_do_block');
  return _to_do_block_user($op, $edit, $account);
}

/**
 * Implementation of hook_block().
 */
function to_do_block_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
  case 'list':
    $block = array();
    $block[0]['info'] = t('To do list');
    $block[0]['cache'] = BLOCK_CACHE_PER_USER;
    // XXX -- add a block with stats
    return $block;

  case 'configure':
    module_load_include('admin.inc', 'to_do_block');
    switch ($delta) {
    case 0:
      return _to_do_block_list_settings_form();

    }
    break;

  case 'save':
    switch ($delta) {
    case 0:
      _to_do_block_list_settings_form_submit($edit);
      break;

    }

  case 'view':
    if (!user_access('can be assigned to dos') && !user_access('create to do content')) {
      return;
    }

    module_load_include('pages.inc', 'to_do_block');
    switch ($delta) {
    case 0:
      return _to_do_block_list();

    }

  }
}

// vim: ts=2 sw=2 et syntax=php
